package cc.telecomdigital.tdstock.Framework.UniqueItem;

import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;

//Internal display Change receiver
public class DisplayChangeListener extends BroadcastReceiver
{
	// For external usage
	public interface IDisplayOnOffChangeListener
	{
		/** Report on/off for the current display status **/
		void DisplayOnOff_Response(boolean iOnOff);
	}

	private boolean					debugMode		= false;
	private final String				LOG_TAG	= "DisplayChangeListener";

	private boolean					displayEnable;

	private IntentFilter				displayFilterAction;

	private Context					hostContext;
	private IDisplayOnOffChangeListener	interfaceReport;
	private boolean					isEnabled;

	/** Create a Display On/Off Change Listener, Broadcast Receiver **/
	public DisplayChangeListener(Context iHostContext,
			IDisplayOnOffChangeListener iReportInterface)
	{
		if (debugMode)
			TDStockLog.d(LOG_TAG, "Create a New Receiver");

		isEnabled = false;
		hostContext = iHostContext;
		interfaceReport = iReportInterface;
		displayEnable = false;

		// Checking for Screen - on off data
		displayFilterAction = new IntentFilter();
		displayFilterAction.addAction(Intent.ACTION_SCREEN_ON);
		displayFilterAction.addAction(Intent.ACTION_SCREEN_OFF);				
	}

	/** Check if it is enabled **/
	public boolean IsEnabled()
	{
		return isEnabled;
	}

	/** Disable the current usage of the Broadcast receiver **/
	public synchronized void Disable()
	{
		if (isEnabled == false)
			return;

		hostContext.unregisterReceiver(this);
		isEnabled = false;
		if (debugMode)
			TDStockLog.d(LOG_TAG, "Unregister Receiver");
	}

	/** Enable the Broadcast Receiver
	   ** Re-Register with the system on **/
	public synchronized void Enable()
	{
		if (isEnabled == true)
			return;

		// *** Very interesting, the one would not send
		hostContext.registerReceiver(this, displayFilterAction);
		// Get the initial value
		displayEnable = ((android.os.PowerManager) hostContext
				.getSystemService(Activity.POWER_SERVICE)).isScreenOn();
		isEnabled = true;
		if (interfaceReport != null)
			interfaceReport.DisplayOnOff_Response(displayEnable);

		if (debugMode)
			TDStockLog.d(LOG_TAG, "Register Receiver");
	}

	/** Dispose the object **/
	public void Dispose()
	{
		Disable();
		if (debugMode)
			TDStockLog.d(LOG_TAG, "Object has been free");
	}

	@Override
	public void onReceive(Context context, Intent iIntent)
	{
		if (debugMode)
			TDStockLog.i(LOG_TAG, "Action: "
					+ iIntent.getAction().toString());

		if (iIntent.getAction().equals(Intent.ACTION_SCREEN_ON))
		{
			// Skip if same value
			if (displayEnable)
				return;
			displayEnable = true;
			if (interfaceReport != null)
				interfaceReport.DisplayOnOff_Response(displayEnable);
			return;
		}

		if (iIntent.getAction().equals(Intent.ACTION_SCREEN_OFF))
		{
			// skip if same value
			if (!displayEnable)
				return;
			displayEnable = false;
			if (interfaceReport != null)
				interfaceReport.DisplayOnOff_Response(displayEnable);
			return;
		}
	}

	/** Ask if the display is on/off
	    The answer could only be accurate, if it is being enabled **/	
	public boolean IsDisplayOn()
	{
		return displayEnable;
	}

	/** Set DebugMessage Level **/
	public void SetDebugMode(boolean iEnable)
	{
		debugMode = iEnable;
	}
}
